//
//  YkSystem.m
//  mcf
//
//  Created by yuike6 yuike6 on 12-1-7.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "YkSystem.h"
#include <mach/mach.h>
#import "header.h"


#define _DEVICE_UUID_FOR_MTOP_              @"device_uuid_for_mtop"

@implementation YkSystem

-(void) dealloc
{
    YRelease(iosVersion);
    YRelease(device_model);
    YRelease(system_name);
    YRelease(build_version);
    YRelease(device_uuid);
    YRelease(device_imei);
    YRelease(device_imsi);
    
    YRelease(device_name)
    [super dealloc];
}

@synthesize iosVersion;
-(NSString *) iosVersion
{
    if(iosVersion == nil ||
       [iosVersion length] <= 0)
    {
        
        iosVersion = [[NSString alloc] initWithString:[[UIDevice currentDevice] systemVersion]]; // 5.0.1
    }
    
    return iosVersion;
}


@synthesize deviceModel = device_model;
- (NSString *) deviceModel
{
    if(device_model == nil ||
       [device_model length] <= 0)
    {
        device_model = [[NSString alloc] initWithString:[[UIDevice currentDevice] model]]; // ipad
    }
    return device_model;
}

@synthesize systemName = system_name;
- (NSString *) systemName
{
    if (system_name == nil ||
        [system_name length] <= 0)
    {
        system_name = [[NSString alloc] initWithString:[[UIDevice currentDevice] systemName]]; // "IOS"
    }
    
    return system_name;
}

@synthesize buildVersion = build_version;
-(NSString *) buildVersion
{    
    if(build_version == nil ||
       [build_version length] <= 0)
    {
        NSString * tmp = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
        build_version = [[NSString alloc] initWithString:tmp];
    }
    return  build_version;
}

@synthesize deviceUuid = device_uuid;
-(NSString *) deviceUuid
{
    if(device_uuid == nil ||
       [device_uuid length] <= 0)
    {
        NSUserDefaults *standardUserDefault = [NSUserDefaults standardUserDefaults]; 
        NSString * deviceIdString = [NSString stringWithFormat:@"%@", [standardUserDefault valueForKey:_DEVICE_UUID_FOR_MTOP_]];
        
        if ([NSString isNilOrEmpty:deviceIdString] ||
            [deviceIdString length] != [[NSString generateUUID] length]) 
        {  
            deviceIdString = [NSString generateUUID];
            
            [standardUserDefault setValue:deviceIdString forKey:_DEVICE_UUID_FOR_MTOP_];              
            [standardUserDefault synchronize];
        }
               
        device_uuid = [[NSString alloc] initWithString:deviceIdString]; // uuuid
    }
    
    return device_uuid;
}

/*
 来自:TOP 
 imei是国际移动设备身份码, imsi是国际移动用户识别码,两个识别码的长度均为 15 位。 对于IPHONE,可以获取UDID,截取前 15 位作为IMEI,后 15 为作为IMSI;对于 ANDROID,IMEI是可以获取到的,IMSI若取不到可以填写成IMEI的内容。
 
 其他无法获取IMEI IMSI的设备,请遵从下述规则:15 个字符, 机器型号(6 个字符) + 以当前时间(2010062557199) 为种子生成的随机数的 16 进制表示(4 个字符)+当前时 间中的秒 (57) + 当前时间中的毫秒(199)
 */
@synthesize deviceImei = device_imei;
-(NSString *) deviceImei
{    
    if(device_imei == nil ||
       [device_imei length] <= 0)
    {
        BOOL canGetDeviceUuid = TRUE;
        NSString * deviceUUID = [NSString stringWithString:self.deviceUuid];
        if([NSString isNilOrEmpty:deviceUUID] ||
           [deviceUUID length] < 15)
        {
            canGetDeviceUuid = FALSE;
        }
        
        if(canGetDeviceUuid)
        {
            NSRange imeiRange;
            imeiRange.location = 0;
            imeiRange.length = 15;
            
            device_imei = [[NSString alloc] initWithString:[deviceUUID substringWithRange:imeiRange]];
        }
        else
        {
            //  其他无法获取IMEI时的情况
        } 
    }
    
    return device_imei;
    
}

@synthesize deviceImsi = device_imsi;
-(NSString *) deviceImsi
{    
    if(device_imsi == nil ||
       [device_imsi length] <= 0)
    {
        BOOL canGetDeviceUuid = TRUE;
        if([NSString isNilOrEmpty:self.deviceUuid] ||
           [self.deviceUuid length] < 15)
        {
            canGetDeviceUuid = FALSE;
        }
        
        if(canGetDeviceUuid)
        {
            NSRange imeiRange;
            imeiRange.length = 15;
            imeiRange.location = [self.deviceUuid length] - imeiRange.length;
            
            device_imsi = [[NSString alloc] initWithString:[self.deviceUuid substringWithRange:imeiRange]];
        }
        else
        {
            //  其他无法获取IMSI时的情况
        } 
    }
    
    return device_imsi;
    
}


@synthesize deviceName = device_name;
- (NSString *) deviceName
{
    if ([NSString isNilOrEmpty:device_name]) {
        device_name = [[NSString alloc] initWithString:[[UIDevice currentDevice] name]]; 
    }    
    
    return device_name;
}



+ (double)getAvailableBytes
{
    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
    
    if (kernReturn != KERN_SUCCESS)
    {
        return NSNotFound;
    }
    
    return (vm_page_size * vmStats.free_count);
}

+ (double)getAvailableKiloBytes
{
    return [YkSystem getAvailableBytes] / 1024.0;
}

+ (double)getAvailableMegaBytes
{
    return [YkSystem getAvailableKiloBytes] / 1024.0;
}



+ (BOOL) isLayoutPortrait
{
    return UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]);
}
+ (BOOL) isLayoutLandscape
{
    return UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
}
@end
